package notice

import (
	"go-papers/models"
)

var (
	OrderPaySuccess    = "1" //订单支付成功
	OrderNeedCheck     = "2" //订单需要排课
	OrderSureSuccess   = "3" // 订单确认排课
	OrderRefund        = "4" // 订单申请 退款
	OrderRefundCheck   = "5" // 订单退款 确认 / 待退款
	OrderRefundSuccess = "6" // 订单退款 成功
	OrderShareFail     = "7" // 订单分账机构 失败
	OrderShareSuccess  = "8" // 订单分账机构 成功

	//具体发送消息模版
	MsgTypeList = map[string]string{
		OrderPaySuccess:    "1",
		OrderNeedCheck:     "1",
		OrderSureSuccess:   "1",
		OrderRefund:        "1",
		OrderRefundCheck:   "1",
		OrderRefundSuccess: "6",
		OrderShareFail:     "7",
		OrderShareSuccess:  "8",
	}
)

func NoticeByType(msgType string, oid int, mid, uid int) {

	msg, _ := MsgTypeList[msgType]
	if msg != "" {
		//进行消息发放
		switch msgType {
		case OrderPaySuccess:
			MsgOrderPaySuccess(msg, oid, 1, mid, uid)
		case OrderNeedCheck:
			MsgOrderPaySuccess1(msg, oid, 1, mid, uid)
		case OrderSureSuccess:
			MsgOrderSureSuccess2(msg, oid)
		case OrderRefund:
			MsgOrderPaySuccess3(msg, oid, 1, mid, uid)
		case OrderRefundCheck:
			MsgOrderPaySuccess1(msg, oid, 1, mid, uid)
		case OrderRefundSuccess:
			MsgOrderPaySuccess6(msg, oid, 1, mid, uid)
		case OrderShareFail:
			MsgOrderShare7(oid, "", "")
		default:

		}
	}
}

func MsgOrderPaySuccess(msg string, oid int, tt, mid, uid int) {

	// 查询订单信息
	var order models.Order
	orderInfo, _ := order.GetByID(oid)
	//
	mm := models.OrganMsg{}
	organId := orderInfo.OrganId

	var orga models.Organ
	organInfo, _ := orga.GetByID(organId)
	// 通知机构管理员
	mList, _ := mm.GetByOrganRole(organId, "10")
	if len(mList) > 0 {
		for _, v := range mList {
			openId := v.OpenId

			//发送 退款成功 微信模版消息
			OrderNew(openId, v.OpenUserName, orderInfo)
		}
	}

	//通知 平台管理员 OrderNew
	mmList, _ := mm.GetByOrganRole(1, "10")
	if len(mmList) > 0 {
		for _, v := range mmList {
			openId := v.OpenId

			//发送 退款成功 微信模版消息
			OrderNew(openId, v.OpenUserName, orderInfo)
		}
	}

	// 通知用户 openId
	OrderNewMember(orderInfo.OpenId, orderInfo.MemberName, orderInfo, organInfo)

	var memb models.Member
	// 拓展用户达人
	if orderInfo.MemberMomId != 0 {
		//查询达人 openid
		momMem, _ := memb.GetByID(orderInfo.MemberMomId)
		OrderNew(momMem.WxOpenid, momMem.RealName, orderInfo)
	}

	// 拓展机构达人
	if organInfo.OrganMomId != 0 {
		//查询达人 openid
		momMem, _ := memb.GetByID(organInfo.OrganMomId)
		OrderNew(momMem.WxOpenid, momMem.RealName, orderInfo)
	}

}

func MsgOrderPaySuccess1(msg string, oid int, tt, mid, uid int) {
	// 类型 是 订单
	if tt == 1 {
		// 查询订单信息

	}
}

func MsgOrderSureSuccess2(MopenId string, oid int) {
	// 类型 是 订单
	// 查询订单信息
	var order models.Order
	orderInfo, _ := order.GetByID(oid)
	//
	mm := models.OrganMsg{}
	organId := orderInfo.OrganId

	// 通知机构管理员
	mList, _ := mm.GetByOrganRole(organId, "10")
	if len(mList) > 0 {
		for _, v := range mList {
			openId := v.OpenId

			//发送 退款成功 微信模版消息
			OrderSure(openId, v.OpenUserName, orderInfo)
		}
	}

	//通知 平台管理员 OrderNew
	mmList, _ := mm.GetByOrganRole(1, "10")
	if len(mmList) > 0 {
		for _, v := range mmList {
			openId := v.OpenId

			//发送
			OrderSure(openId, v.OpenUserName, orderInfo)
		}
	}

	// 通知用户 openId
	OrderSure(MopenId, orderInfo.MemberName, orderInfo)

}

func MsgOrderPaySuccess3(msg string, oid int, tt, mid, uid int) {
	// 类型 是 订单
	// 查询订单信息
	var order models.Order
	orderInfo, _ := order.GetByID(oid)
	//
	mm := models.OrganMsg{}
	organId := orderInfo.OrganId

	// 通知机构管理员
	mList, _ := mm.GetByOrganRole(organId, "10")
	if len(mList) > 0 {
		for _, v := range mList {
			openId := v.OpenId

			//发送 退款成功 微信模版消息
			OrderRefundMsg(openId, v.OpenUserName, orderInfo)
		}
	}

	//通知 平台管理员 OrderNew
	mmList, _ := mm.GetByOrganRole(1, "10")
	if len(mmList) > 0 {
		for _, v := range mmList {
			openId := v.OpenId

			//发送 退款成功 微信模版消息
			OrderRefundMsg(openId, v.OpenUserName, orderInfo)
		}
	}

}

func MsgOrderPaySuccess6(msg string, oid int, tt, mid, uid int) {
	// 类型 是 订单
	var sysu models.SysUser
	userInfo, _ := sysu.GetById(uid)
	// 查询订单信息
	var orr models.Order
	orderInfo, _ := orr.GetByID(oid)
	//通知系统管理员
	mm := models.OrganMsg{}
	mmList, _ := mm.GetByOrganRole(1, "10")
	if len(mmList) > 0 {
		for _, v := range mmList {
			openId := v.OpenId
			name := userInfo.Username

			//发送 退款成功 微信模版消息
			RefundAdmin(openId, v.OpenUserName, name, orderInfo)
		}
	}
}

func MsgOrderShare7(oid int, price, errormsg string) {
	// 类型 是 订单
	// 查询订单信息
	var order models.Order
	orderInfo, _ := order.GetByID(oid)
	//
	mm := models.OrganMsg{}
	//通知 平台管理员 OrderNew
	mmList, _ := mm.GetByOrganRole(1, "10")
	if len(mmList) > 0 {
		for _, v := range mmList {
			openId := v.OpenId

			//发送 退款成功 微信模版消息
			OrderShareMsg(openId, v.OpenUserName, orderInfo, price, errormsg)
		}
	}
}
